package com.love.marriage.partnerCategoryConfig.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.common.exceptions.SystemException;
import com.love.marriage.partnerCategoryConfig.entity.PartnerCategoryConfig;
import com.love.marriage.partnerCategoryConfig.mapper.PartnerCategoryConfigMapper;
import com.love.marriage.partnerCategoryConfig.service.PartnerCategoryConfigService;
import com.love.marriage.partnerCategoryConfig.vo.PartnerCategoryConfigVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  合伙人类别配置表服务实现类
 * author:       mybatisPlus
 * createDate:   2025-02-11 07:18:51
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class PartnerCategoryConfigServiceImp extends ServiceImpl<PartnerCategoryConfigMapper, PartnerCategoryConfig> implements PartnerCategoryConfigService {

    private final PartnerCategoryConfigMapper partnerCategoryConfigMapper;

    @Override
    public Page<PartnerCategoryConfigVo> getPage(Long pageNum, Long pageSize, PartnerCategoryConfigVo partnerCategoryConfigVo) {
        Page<PartnerCategoryConfigVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return partnerCategoryConfigMapper.getPage(page, partnerCategoryConfigVo);
    }

    @Override
    public List<PartnerCategoryConfigVo> getList(PartnerCategoryConfigVo partnerCategoryConfigVo) {
        return partnerCategoryConfigMapper.getList(partnerCategoryConfigVo);
    }

    @Override
    public PartnerCategoryConfigVo queryPartnerCategoryConfig(Long id) {
        return partnerCategoryConfigMapper.queryPartnerCategoryConfig(id);
    }

    @Override
    public PartnerCategoryConfigVo addPartnerCategoryConfig(PartnerCategoryConfigVo partnerCategoryConfigVo) {
        if (queryPartnerCategoryConfigByCode(partnerCategoryConfigVo.getCategoryCode(), null)) {
            throw new SystemException("1200001", "该编码已存在");
        }
        PartnerCategoryConfig partnerCategoryConfig = new PartnerCategoryConfig();
        BeanUtils.copyProperties(partnerCategoryConfigVo, partnerCategoryConfig);
        partnerCategoryConfigMapper.insert(partnerCategoryConfig);
        PartnerCategoryConfigVo res = new PartnerCategoryConfigVo();
        BeanUtils.copyProperties(partnerCategoryConfig, res);
        return res;
    }

    @Override
    public PartnerCategoryConfigVo updatePartnerCategoryConfig(PartnerCategoryConfigVo partnerCategoryConfigVo) {
        if (queryPartnerCategoryConfigByCode(partnerCategoryConfigVo.getCategoryCode(), partnerCategoryConfigVo.getId())) {
            throw new SystemException("1200001", "该编码已存在");
        }
        PartnerCategoryConfig partnerCategoryConfig = new PartnerCategoryConfig();
        BeanUtils.copyProperties(partnerCategoryConfigVo, partnerCategoryConfig);
        partnerCategoryConfigMapper.updateById(partnerCategoryConfig);
        return partnerCategoryConfigVo;
    }

    @Override
    public Boolean deletePartnerCategoryConfig(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        partnerCategoryConfigMapper.deleteBatchIds(idArr);
        return true;
    }

    // 添加根据编码查询配置的方法
    public Boolean queryPartnerCategoryConfigByCode(String code, Long id) {
        List<PartnerCategoryConfig> list = this.list(
                Wrappers.<PartnerCategoryConfig>lambdaQuery()
                        .eq(PartnerCategoryConfig::getCategoryCode, code)
                        .ne(id != null, PartnerCategoryConfig::getId, id)
        );
        return list != null && list.size() > 0;
    }
}
